﻿using System.Linq;
using HP.Cachalote.DataModels;
using HP.Cachalote.Repositories.Interfaces;
using HP.Cachalote.Services.Interfaces;
using HP.Cachalote.Services.Validations;
using HP.Cachalote.Services.Shared;

namespace HP.Cachalote.Services
{
    public enum TodoItemFilterType
    {
        All,
        Today,
        Tomorrow,
        Next,
        Overdue
    }

    public class TodoItemService : ServiceBase<TodoItem>, ITodoItemService
    {
        public TodoItemService(IRepositoryFactory repositoryFactory) : base(repositoryFactory, new TodoItemValidator()) { }

        // <summary>
        /// Get TodoItem by id. It will return null if the TodoItem does not exist.
        /// </summary>
        /// <param name="id">The specified id.</param>
        /// /// <param name="loadTodoLabel">True to load related TodoLabel at the same time. False NOT load.</param>
        /// <returns>The matched TodoItem.</returns>
        public TodoItem Get(object id, bool loadTodoLabel)
        {
            using (RepositoryFactory)
            {
                RepositoryFactory.OpenSession();
                return RepositoryFactory.CreateTodoItemRepository().Get(id, loadTodoLabel);
            }
        }

        /// <summary>
        /// Filter TodoItem by lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts TodoItem and return boolean.</param>
        /// <param name="loadTodoLabel">True to load related TodoLabel at the same time. False NOT load.</param>
        /// <returns>An IQueryable list of TodoItem.</returns>
        public System.Collections.Generic.IList<TodoItem> Filter(System.Linq.Expressions.Expression<System.Func<TodoItem, bool>> expression, bool loadTodoLabel)
        {
            using (RepositoryFactory)
            {
                RepositoryFactory.OpenSession();
                return RepositoryFactory.CreateTodoItemRepository().Filter(expression, loadTodoLabel).ToList();
            }
        }
    }
}
